package com.badshun.player;

import com.badlogic.gdx.graphics.*;
import com.badshun.box2d.*;
import com.badlogic.gdx.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.graphics.g2d.*;
import java.util.*;
import com.badshun.ui.*;

public class NPCPlayer {
	private MainPlayer mainPlayer;
	
	private Texture headImage;
	private String name;
	private ArrayList<TalkBox> talkArray;
	
	public ACTION action;
	public DIRECTION direction;
	
	public boolean isHitPlayer;
	
	private Box2DWorld world;
	
	private float width;
	private float height;
	
	private Vector2 position;
	
	public Body npcBody;
	
	private TextureRegion keyFrame;
	
	private Animation leftAnimation;
	private Animation rightAnimation;
	private Animation upAnimation;
	private Animation downAnimation;
	
	private SpriteBatch batch;
	
	private OrthographicCamera camera;

	private float time;
	
	public String[] talkContent;
	
	private int index = 0;
	
	private TalkBox talkBox;
	
	private String TAG;

	public NPCPlayer(String TAG,MainPlayer mainPlayer,Texture headImage, String name, ACTION action, DIRECTION direction,Box2DWorld world,String playerImagePath,Vector2 position,OrthographicCamera camera,String[] talkContent) {
		this.TAG = TAG;
		this.mainPlayer = mainPlayer;
		this.headImage = headImage;
		this.name = name;
		this.talkArray = new ArrayList<TalkBox>();
		this.action = action;
		this.direction = direction;
		this.world = world;
		this.position = new Vector2((position.x + width / 2) / 2,(position.y + height / 2) / 2);
		this.camera = camera;
		this.talkContent = talkContent;
		
		talkArray = new ArrayList<TalkBox>();
		
		batch = new SpriteBatch();
		
		defineNPC(playerImagePath,position);
	}
	
	private void defineNPC(String playerImagePath,Vector2 position) {
		Texture texture = new Texture(Gdx.files.internal(playerImagePath));
		
		width = texture.getWidth() / 4;
		height = texture.getHeight() / 4;
		
		BodyDef bdef = new BodyDef();
		bdef.position.set(position.x,position.y);
		bdef.type = BodyDef.BodyType.StaticBody;

		npcBody = world.world.createBody(bdef);
		npcBody.setUserData(TAG + "NPC");

		PolygonShape shape = new PolygonShape();
		shape.setAsBox(width / 2,height / 2);

		FixtureDef fdef = new FixtureDef();
		fdef.shape = shape;
		fdef.filter.categoryBits = 4;
		fdef.filter.maskBits = 2 | 8 | 16;

		npcBody.createFixture(fdef).setUserData(name);
		
		TextureRegion[][] npcTextureRegionList = TextureRegion.split(texture,texture.getWidth() / 4,texture.getHeight() / 4);

		leftAnimation = new Animation(0.08f,npcTextureRegionList[1]);
		rightAnimation = new Animation(0.08f,npcTextureRegionList[2]);
		upAnimation = new Animation(0.08f,npcTextureRegionList[3]);
		downAnimation = new Animation(0.08f,npcTextureRegionList[0]);

		leftAnimation.setPlayMode(Animation.PlayMode.LOOP);
		rightAnimation.setPlayMode(Animation.PlayMode.LOOP);
		upAnimation.setPlayMode(Animation.PlayMode.LOOP);
		downAnimation.setPlayMode(Animation.PlayMode.LOOP);
		
		createTalkArray();
	}
	
	private void createTalkArray() {
		if(talkArray.size() > 0) {
			return ;
		} else {
			for(int i = 0; i < talkContent.length; i++) {
				TalkBox talkBox = null;

				if(talkContent[i].startsWith("+")) {
					talkBox = new TalkBox(headImage,name,talkContent[i].replace("+",""));
				} else if(talkContent[i].startsWith("-")) {
					talkBox = new TalkBox(mainPlayer.headImage,mainPlayer.name,talkContent[i].replace("-",""));
				}

				talkArray.add(talkBox);
			}
		}
	}
	
	public void next() {
		if(index < talkArray.size()) {
			talkBox = talkArray.get(index);

			index++;
		} else {
			index = 0;
			
			talkBox = null;
		}
	}
	
	private void checkDirection() {
		if(npcBody.getLinearVelocity().x == 0 && npcBody.getLinearVelocity().y == 0) action = ACTION.IDLE;
		if(npcBody.getLinearVelocity().x < 0 && npcBody.getLinearVelocity().y == 0) direction = DIRECTION.LEFT;
		if(npcBody.getLinearVelocity().x > 0 && npcBody.getLinearVelocity().y == 0f) direction = DIRECTION.RIGHT;
		if(npcBody.getLinearVelocity().x == 0 && npcBody.getLinearVelocity().y > 0) direction = DIRECTION.UP;
		if(npcBody.getLinearVelocity().x == 0 && npcBody.getLinearVelocity().y < 0) direction = DIRECTION.DOWN;
	}
	
	public TextureRegion getKeyFrame(float time) {
		if(direction == DIRECTION.LEFT && action == ACTION.MOVE) return leftAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.RIGHT && action == ACTION.MOVE) return rightAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.UP && action == ACTION.MOVE) return upAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.DOWN && action == ACTION.MOVE) return downAnimation.getKeyFrame(time);
		else if(action == ACTION.IDLE) {
			if(direction == DIRECTION.LEFT) return leftAnimation.getKeyFrames()[0];
			else if(direction == DIRECTION.RIGHT) return rightAnimation.getKeyFrames()[0];
			else if(direction == DIRECTION.UP) return upAnimation.getKeyFrames()[0];
			else if(direction == DIRECTION.DOWN) return downAnimation.getKeyFrames()[0];
		}

		return null;
	}

	public Texture getHeadImage() {
		return headImage;
	}

	public String getName() {
		return name;
	}
	
	public ACTION getAction() {
		return action;
	}

	public DIRECTION getDirection() {
		return direction;
	}
	
	public void render() {
		position.set(npcBody.getPosition().x - width / 2,npcBody.getPosition().y - height / 2);
		
		checkDirection();
		
		if(talkBox != null) {
			talkBox.render();
		}
		
		batch.setProjectionMatrix(camera.combined);
		batch.begin();

		keyFrame = getKeyFrame(time);
		
		batch.draw(keyFrame,position.x,position.y,width,height);
		
		batch.end();
		
		time += Gdx.graphics.getDeltaTime();
	}
	
	public enum ACTION {
		MOVE,TALK,IDLE
	}

	public enum DIRECTION {
		LEFT,RIGHT,UP,DOWN
	}
}
